package com.example.ch04_03toastdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button btn_default;
	private Button btn_customPosition;
	private Button btn_customLayoutWithXML;
	private Button btn_customLayoutWithHardCoding;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initHealder();
	}

	private void initHealder() {
		// TODO Auto-generated method stub
		btn_default.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "Default Toast",
						Toast.LENGTH_SHORT).show();
			}
		});
		btn_customPosition.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast toast = Toast.makeText(MainActivity.this,
						"Custom Position Toast", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		});
		btn_customLayoutWithXML.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast toast = new Toast(MainActivity.this);
				toast.setGravity(Gravity.CENTER | Gravity.LEFT, 0, 0);
				LayoutInflater inflater = getLayoutInflater();
				View view = inflater.inflate(R.layout.toast_my,
						(ViewGroup) findViewById(R.id.rootLayout));
				toast.setView(view);
				toast.setDuration(200);
				toast.show();
			}
		});
		btn_customLayoutWithHardCoding
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Toast toast = Toast.makeText(MainActivity.this,
								MainActivity.this
										.getString(R.string.hello_world),
								Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER | Gravity.RIGHT, 0, 0);
						LinearLayout layout = (LinearLayout) toast.getView();
						ImageView imgView = new ImageView(MainActivity.this);
						imgView.setImageResource(R.drawable.dragon);
						layout.addView(imgView, 0);
						toast.show();
					}
				});
	}

	private void initView() {
		// TODO Auto-generated method stub
		btn_default = (Button) this.findViewById(R.id.btn_default);
		btn_customPosition = (Button) this
				.findViewById(R.id.btn_customPosition);
		btn_customLayoutWithXML = (Button) this
				.findViewById(R.id.btn_customLayoutWithXML);
		btn_customLayoutWithHardCoding = (Button) this
				.findViewById(R.id.btn_customLayoutWithHardCoding);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
